package com.xmy.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.study.entity.EduVersion;
import com.xmy.study.service.IEduVersionService;
import com.xmy.study.util.Code;
import com.xmy.study.util.IdWorker;
import com.xmy.study.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * <p>
 * 课程版本 前端控制器
 * </p>
 *
 * @author Coly
 * @since 2023-05-29
 */
@RestController
@RequestMapping("/api/admin/eduVersion")
public class EduVersionController {

    @Autowired
    IdWorker idWorker;

    @Autowired
    IEduVersionService eduVersionService;


    @PostMapping("newVersion")
    @ApiOperation("新增版本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "学段名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序", required = true, dataType = "String")
    })
    public Result saveVersion(@RequestBody Map reMap) {
        String name = (String) reMap.get("name");
        String sort = (String) reMap.get("sort");

        EduVersion eduVersion = new EduVersion();
        eduVersion.setId(idWorker.nextId());
        eduVersion.setName(name);

        if (StringUtils.isNotBlank(sort)) {
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            Boolean sortB = pattern.matcher(sort).matches();
            if (!sortB) {
                return new Result(Code.SAVE_ERROR, Code.SAVE_ERROR_MSG, "请正确输入排序，例如：1");
            }
            eduVersion.setSort(sort);
        }
        eduVersion.setStatus(1);
        Boolean save = eduVersionService.save(eduVersion);
        return new Result(save ? Code.SAVE_OK : Code.SAVE_ERROR, null, save ? Code.SAVE_OK_MSG : Code.SAVE_ERROR_MSG);
    }

    @GetMapping("getVersion")
    @ApiOperation("查询版本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "学段名", required = true, dataType = "String")
    })
    public Result searchPeriod(@RequestParam("name") String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderByAsc("sort");
        List<EduVersion> eduVersionList = eduVersionService.list(queryWrapper);
        return new Result(Code.GET_OK, eduVersionList, Code.GET_OK_MSG);
    }

    @DeleteMapping("delVersion")
    @ApiOperation("删除版本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "名称", required = true, dataType = "String")
    })
    public Result removeVersion(@RequestParam("id") String id) {
        EduVersion eduVersion = eduVersionService.getById(id);
        if (Objects.isNull(eduVersion)) {
            return new Result(Code.EXP_ERROR, Code.EXP_MSG, "该版本信息不存在");
        }
        Boolean removeById = eduVersionService.removeById(id);
        return new Result(removeById ? Code.DELETE_OK : Code.DELETE_ERROR, null, removeById ? Code.DELETE_OK_MSG : Code.DELETE_OK_MSG);
    }

    @PostMapping("updateVersion")
    @ApiOperation("修改版本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "学段名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序", required = false, dataType = "String")
    })
    public Result updateVersion(@RequestBody Map reMap) {
        String id = (String) reMap.get("id");
        String name = (String) reMap.get("name");
        String sort = (String) reMap.get("sort");
        EduVersion eduVersion = eduVersionService.getById(id);
        if (Objects.isNull(eduVersion)) {
            return new Result(Code.EXP_ERROR, Code.EXP_MSG, "该版本信息不存在");
        }

        eduVersion.setName(name);

        if (Objects.nonNull(sort)) {
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            Boolean sortB = pattern.matcher(sort).matches();
            if (!sortB) {
                return new Result(Code.SAVE_ERROR, Code.SAVE_ERROR_MSG, "请正确输入排序，例如：1");
            }
            eduVersion.setSort(sort);
        }
        Boolean updateById = eduVersionService.updateById(eduVersion);
        return new Result(updateById ? Code.UPDATE_OK : Code.UPDATE_ERROR, null, updateById ? Code.UPDATE_OK_MSG : Code.UPDATE_ERROR_MSG);
    }
}
